package com.WOWTradeNews;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.util.Log;

public class Utils {
	public final static int IO_BUFFER_SIZE=512;	
	
	public static Intent getPdfFileIntent(String param){
		Uri uri = Uri.parse(param); 
		Intent intent  = new Intent(Intent.ACTION_VIEW, uri); 
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		return intent;
	}
	
	public static String toUtf8(String str) {
		if (str == null)
			return null;
		try {
			return new String(str.getBytes("8859_1"), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
    public static Bitmap getUrlBitmapBonus(String streamURL){
    	Log.d("zhaosdfa", "getUrlBitmap---->>>" + streamURL);
        InputStream in = null;
        BufferedOutputStream out = null;
        Bitmap bitmap=null;
        
		try{
	    	//fileName=Utils.escapeConvert(fileName);
	    	
	    	System.out.println(streamURL);
    		
	        in = new BufferedInputStream(new URL(streamURL).openStream(), IO_BUFFER_SIZE);	        
			ByteArrayOutputStream dataStream = new ByteArrayOutputStream();			
			out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);	        
			Utils.copy(in, out);
			out.flush();
			in.close();
						
			final byte[] data = dataStream.toByteArray();			
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = false;
			options.inSampleSize = 1;
			bitmap=BitmapFactory.decodeByteArray(data, 0, data.length, options);
		}
		catch(Exception e){
			System.out.println(e);
		} 
		
		return bitmap;
    }
    
    public static String escapeConvert(String s){
    	if(s.indexOf(" ")!=-1){
    		s=s.replaceAll(" ", "%20");
    	}
    	if(s.indexOf("(")!=-1){
    		s=s.replaceAll("\\(", "%28");
    	}
    	if(s.indexOf(")")!=-1){
    		s=s.replaceAll("\\)", "%29");
    	}
    	return s;
    }
    
	public static void copy(InputStream in, OutputStream out) throws IOException {
		
		byte[] b = new byte[IO_BUFFER_SIZE];
		int read;
		while ((read = in.read(b)) != -1) {
			out.write(b, 0, read);
		}
	}
	
	public static byte[] readInputStream(InputStream in) throws IOException {
		
		byte[] b = new byte[IO_BUFFER_SIZE];
		int read;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		while ((read = in.read(b)) != -1) {
			out.write(b, 0, read);
		}
		
		return out.toByteArray();
	}
	
	public static boolean isConnect(Activity activity) {
		boolean flag = false;
		ConnectivityManager cwjManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cwjManager.getActiveNetworkInfo() != null)
			flag = cwjManager.getActiveNetworkInfo().isAvailable();
		return flag;
	}
}
